#ifndef DRAGFRAME_H
#define DRAGFRAME_H

#include <QFrame>
#include <QLabel>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QGridLayout>
#include <QSettings>
#include <QDebug>
#include <QCoreApplication>
#include <QtMath>
#include <QDateTime>
#include <QJsonObject>
#include <QJsonDocument>

#pragma pack(push,2)  // 字节对齐
typedef union CardInfo    // MSA9020卡数据信息
{
    struct
    {
        qint16 v_real_cha;          // -9999~9999
        quint16 decimal_cha;        // 0,1,2 小数位数
        qint16 v_real_chb;
        quint16 decimal_chb;
        // CHA
        quint16 state_fail_cha;     // 是否有故障   0无故障 1～8故障F1～F8
        quint16 state_drift_cha;    // 是否超漂移   0无漂移 <>0漂移
        quint16 state_eeee_cha;     // 是否超量程   0没超量程 1超量程EEEE
        quint16 state_c_cha;        // 是否小心报警 0无小心报警 1产生小心报警
        quint16 state_w_cha;        // 是否预报警   0无预报警 1产生预报警
        quint16 state_a_cha;        // 是否高报警   0无高报警 1产生高报警
        quint16 e1_cha;     // 0=no error E1, 1=in error E1，零信号和满量程之间的信号差 <2mV
        quint16 e2_cha;     // 0=no error E2, 1=in error E2，零信号和满量程之间的信号差 >=200mV
        quint16 e3_cha;     // 0=no error E3, 1=in error E3，零信号和满量程之间的信号差 3mV>n>2mV
        quint16 e4_cha;     // 0=no error E4, 1=in error E4，输入信号高于 +300mV
        // CHB
        quint16 state_fail_chb;     // 是否有故障
        quint16 state_drift_chb;    // 是否超漂移
        quint16 state_eeee_chb;     // 是否超量程
        quint16 state_c_chb;        // 是否小心报警
        quint16 state_w_chb;        // 是否预报警
        quint16 state_a_chb;        // 是否高报警
        quint16 e1_chb;
        quint16 e2_chb;
        quint16 e3_chb;
        quint16 e4_chb;
    };
    quint16 data[24];
} CARD_INFO;
#pragma pack(pop)   // 字节对齐END

namespace Ui
{
class DragFrame;
}

class DragFrame: public QFrame
{
    Q_OBJECT

public:
    explicit DragFrame(QWidget* parent, int id);
    //~DragFrame();

    void setModbusAddr(int addr); // 设置Modbus地址
    void setChabName(QString cha, QString chb) // 设置AB通道传感器名称
    {
        addrA->setText(cha);
        addrB->setText(chb);
        chaAddr = cha;
        chbAddr = chb;
    }
    void setUnitA(quint16 value);  // A通道单位
    void setUnitB(quint16 value);  // B通道单位
    void setIndex(int id)
    {
        fid = id;
    }
    void dispTimeout();    // 显示通道超时状态
    double  getValueA(); //获取浓度值
    double  getValueB();
    QString getUnitA();  //获取浓度单位
    QString getUnitB();
    int     getModbusAddr();
    bool    getAlarming();

signals:
    void playWarningSound();
    void sendMqtt(int id, QString json);

public slots:
    void setValues(QVector<quint16> values);   // 为CardInfo结构赋值

private:
    QGridLayout* gridLayout;
    QLabel* modbusAddr;
    QLabel* addrA, *addrB;
    QLabel* cha_value, *chb_value;
    QLabel* al, *ah, *ac, *af;
    QLabel* bl, *bh, *bc, *bf;

    int old_e1_cha = 0, old_e1_chb = 0, old_e2_cha = 0, old_e2_chb = 0, old_e3_cha = 0, old_e3_chb = 0,
        old_e4_cha = 0, old_e4_chb = 0;
    int old_state_c_cha = 0, old_state_c_chb = 0, old_state_a_cha = 0, old_state_a_chb = 0,
        old_state_w_cha = 0, old_state_w_chb = 0;
    int old_state_eeee_cha = 0, old_state_eeee_chb = 0, old_state_drift_cha = 0,
        old_state_drift_chb = 0, old_state_fail_cha = 0, old_state_fail_chb = 0;

    CARD_INFO ci;
    QString unitStr[6] = {"ELE", "ELEm", "PPM", "%", "g/m3", ""};  // 单位符号
    QString unit_a = "", unit_b = ""; // A B通道单位符号
    QString chaAddr = "", chbAddr = "";
    double value_a, value_b;// A B通道浓度值
    int fid, fmodbusAddr;   // QFrame（卡槽）编号
    QHash<QString, QString> F1_8;
    QJsonObject chJson;

    void dbSaveRecode(QString chname, double value, QString unit); // 保存浓度值
    void dbSaveAlarm(QString chname, QString alarmStr); // 保存报警信息

};

#endif // DRAGFRAME_H
